package com.hayq.readcard.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig  implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket处理器
        registry.addHandler(new CardWebSocketHandler(), "/ws/card")
                .addInterceptors(new HttpSessionHandshakeInterceptor()) // 添加握手拦截器（可选）
                .setAllowedOrigins("*"); // 允许跨域请求
    }

    @Bean
    public CardWebSocketHandler cardWebSocketHandler() {
        return new CardWebSocketHandler();
    }
}
